/**
 * Created by IntelliJ IDEA.
 * User: bilesja
 * Date: 4/26/11
 * Time: 12:09 AM
 * To change this template use File | Settings | File Templates.
 */
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*; //get access to all of the classes (*) in the java.util package

public class FileCoordinateScanner {


	public static Hash scanner(JFrame frame, Hash hash){
        boolean fileFound = false;
        File textDoc = null;
        while (fileFound == false){
            JFileChooser fc = new JFileChooser();
            fc.setFileHidingEnabled(true);
            fc.setFileFilter(new TextFilter());
            fc.setDialogTitle("Please select a text file");
            fc.showOpenDialog(frame);
            textDoc = fc.getSelectedFile();
            System.out.println(textDoc);
            try {
                Scanner input = new Scanner(textDoc);
                while( input.hasNext() ) {  //whitespace delimited (space, tab, newline, etc.)
                    hash.addCoordinate((int)(100*Double.parseDouble(input.next())), (int)(100*Double.parseDouble(input.next())));
                }
                fileFound = true;
            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(frame, "Next time, please use a text file");
                e.printStackTrace();
            } catch (IndexOutOfBoundsException f){
                JOptionPane.showMessageDialog(frame, "Next time, please use a text file (IndexOutOfBoundsException)");
                f.printStackTrace();
            }
            if (hash.map.size() == 0){
                fileFound = false;
                JOptionPane.showMessageDialog(frame, "Please pick a file with coordinates in it.");
            }

        }
		return hash;
	}

}
